<?php
if ($statisticsOption == 'week'):
    
    $week = date('W', $fromDate);
    $d = Util::getDaysInWeek($week, date('Y', time()));

    $xAxis = "[";
    $amounts = "[";
    for ($i = 1; $i <= 7; $i++):
        $xAxis .= $i . ", ";
        $amounts .= menuService::getAmountRevenueByWeek($d[$i - 1], ($d[$i - 1] + 86399), $user) . ", ";        
    endfor;
    $xAxis = substr_replace($xAxis, '', -2) . "]";
    $amounts = substr_replace($amounts, '', -2) . "]";
elseif ($statisticsOption == 'month'):
    //get total days in month
    $daysOfMonth = cal_days_in_month(CAL_GREGORIAN, date('m', $fromDate), date('Y', $fromDate));
    //init x - y in column chart
    unset($xAxis);
    unset($amounts);
    $xAxis = "[";
    
    //add horizontal value in column chart
    for ($i = 1; $i <= $daysOfMonth; $i++):
        $xAxis .= $i . ", ";
    endfor;
    $xAxis = substr_replace($xAxis, '', -2) . "]";

    $statistcsOrders = menuService::getAmountRevenueByMonth($fromDate, $user);
    foreach ($statistcsOrders as $order => $value):
        $statisticsArr[date('d', strtotime($value->statisticsDate))] = $value->totalPrice;    //$$statisticsArr[day_number_in_month] = total_cups_coffee
    endforeach;

    $amounts = "[";
    for ($j = 1; $j <= $daysOfMonth; $j++):
        $numDate = $j < 10 ? '0' . $j : $j;
        if (isset($statisticsArr[$numDate])):
            $amounts .= $statisticsArr[$numDate] . ", ";
        else:
            $amounts .= 0 . ", ";
        endif; 
        
    endfor;
    $amounts = substr_replace($amounts, '', -2) . "]";    
elseif ($statisticsOption == 'year'):
    //init x - y in column chart
    unset($xAxis);
    unset($amounts);
    $xAxis = "[";
    
    //add horizontal value in column chart
    for ($i = 1; $i <= 12; $i++):
        $xAxis .= $i . ", ";
    endfor;
    $xAxis = substr_replace($xAxis, '', -2) . "]";
        
    $statistcsOrders = menuService::getAmountRevenuByYear($fromDate, $user);
    foreach ($statistcsOrders as $order => $value):
        $statisticsArr[date('m', strtotime($value->statisticsMonth))] = $value->totalPrice;
    endforeach;
    
    $amounts = "[";
    for ($j = 1; $j <= 12; $j++):
        $numMonth = $j < 10 ? '0' . $j : $j;
        if (isset($statisticsArr[$numMonth])):
            $amounts .= $statisticsArr[$numMonth] . ", ";
        else:
            $amounts .= 0 . ", ";
        endif;        
    endfor;
    $amounts = substr_replace($amounts, '', -2) . "]";      
endif;

Yii::app()->clientScript->registerScript("line_chart_22121", "
$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'statisticsRevenus',
                type: 'line',
                marginRight: 130,
                marginBottom: 25
            },
            title: {
                text: 'Statistics Revenus',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: central.fresco.com',
                x: -20
            },
            xAxis: {
                categories: " . $xAxis . "
            },
            yAxis: {
                title: {
                    text: 'Revenue'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        this.x +': '+ this.y +' VND';
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: [{
                name: 'Coffee',
                data: " . $amounts . "
            }]
        });
    });
    
});    
", CClientScript::POS_HEAD);
?>
<div style="background: #FFF; margin-bottom: 10px; padding: 10px">
    <?php $this->renderPartial('_search', array('fromDate' => $fromDate, 'toDate' => $toDate)) ?>
</div>
<div id="statisticsRevenus" style="min-width:78%;height: 400px; margin: 0 auto"></div>

